<?php


class login extends controllers {
    function __construct() {
        parent::__construct();
         //验证码
        define("VALID_NUMBER", "<img id='valid_src' src='" . SITE_URL . "login/validn/?f=" . rand(0, 1000) . "' onclick=\"this.src='" . SITE_URL . "login/validn/?f='+Math.random()\" style='cursor:pointer;'  />");
    }
    function index() {
        if( ! empty($_SESSION['adminmanager']) ){
            redirect(SITE_URL.'admin/index');
        }

        if(isset($_POST['login'])){
            $this->loadModel('g5_admin_user');
            $login              = $_POST['login'];
            $login['user_pwd']  = md5($login['user_pwd']);
            $user               = $this->g5_admin_user->getOne('*', $login);
            if( empty($user) ){
                $this->report($this->systemLang['userNameFail'], '/login/index');
            }
            if($_POST['validn'] != $_SESSION['validn']){
            	$this->report($this->systemLang['verificationFail'], '/login/index');
            }
            if($user['user_status'] == 0){
                $this->report($this->systemLang['userAceessForbid'],'/login/index');
            }
            if($user['user_pwd'] === $login['user_pwd']){
                $info = $this->g5_admin_user->getUser($user['user_id']);
                $_SESSION['adminmanager'] = $info;
                
                //获取可以查看的游戏权限
                $this->loadModel('g5_admin_group');
                $group = $this->g5_admin_group->getOne('*', array('group_id'=>$info['group_id']));
                if($group['group_game']){
                	$_SESSION['adminmanager']['gameList'] =explode(',',$group['group_game']);
                }
                
                $_COOKIE['USERID']   = $user['user_name'];
                $_COOKIE['KEEPLIVE'] = md5($user['user_pwd']);
                $log = array(
                        'uid'=>$user['user_id'],
                        'uri'=>'login/index',
                        'time'=>time(),
                        'description'=>$this->systemLang['userLogin'],
                        'ip' => getClientIP()
                    );
                $this->loadModel('g5_admin_log');
                $this->g5_admin_log->insert($log);
                redirect(SITE_URL.'admin/index');
            }else{
                $this->report($this->systemLang['userNameFail'],'/login/index');
            }
        }
        $this->display('login');
    }

    function loginout() {
        $log = array(
                        'uid'=>$_SESSION['adminmanager']['user_id'],
                        'uri'=>'login/loginout',
                        'time'=>time(),
                        'description'=>$this->systemLang['userLogout'],
                        'ip' => getClientIP()
                    );
        $this->loadModel('g5_admin_log');
        $this->g5_admin_log->insert($log);

        unset($_SESSION['adminmanager']);
        redirect(SITE_URL.'login/index');
    }

    //跳转页面
	public function report($message = '', $url = '', $auto_refresh = true){
		//查看cookie返回页面
		if( ! empty($_COOKIE['referer'])){
			$referer = $_COOKIE['referer'];
			unset($_COOKIE['referer']);
			setcookie('referer', '', -86400);
			$url = empty($url) ? $referer : $url;
		}

		$this->message = $message;
		$this->url = empty($url) ? SITE_URL : $url;
		$this->auto_refresh = $auto_refresh;
		$this->display('redirect');
	}

    function getChannel(){
        $channelList = $this->loadModel('channels')->channelList();

        echo json_encode($channelList);
    }


    /**
     * 重签计划任务
     * @author Giles <giles.wang@icloud.com>
     * @return void
     */
    function sign()
    {
        //ignore_user_abort(true);
        $this->loadModel('g5_cron_job');
        $this->loadModel('g5_perfile');
        $this->loadModel('g5_original_pack');
        $check = $this->g5_cron_job->getCount('*', array('status' => gamesign::STATUS_TWO));

        if($check != 0) {
            saveLog('有打包任务跳过');
            return ;
        }
        $where = array('status' => gamesign::STATUS_ONE);
        $result = $this->g5_cron_job->limit(3, 0)->getAll('*', $where);
        foreach($result as $val) {

            $perfileInfo = $this->g5_perfile->getOne('*', array('id' => $val['perfileId']));
            $packInfo    = $this->g5_original_pack->getOne('packName', array('id' => $val['packId']));

            $this->g5_cron_job->update(array('status' => gamesign::STATUS_TWO), array('id' => $val['id']));
            $newPakName = package::reSign($val, $packInfo, $perfileInfo);

            if($newPakName['error'] != '0000') {
                saveLog('打包失败：'.$packInfo['packName']);
                $this->g5_cron_job->update(array('status' => gamesign::STATUS_FOR, 'errorinfo' => $newPakName['message']), array('id' => $val['id']));
            } else {
                $up = array(
                    'packSize' => format_bytes(filesize(TRUE_PAK.$newPakName['message'])),
                    'packName' => $newPakName['message'],
                    'trueTime' => time(),
                    'status'   => gamesign::STATUS_SWH
                );
                saveLog('打包成功：'.$packInfo['packName']);
                $this->g5_cron_job->update($up, array('id' => $val['id']));
            }
        }
    }

    /**
     * 检测证书是否被封 以及新证书添加
     */
    function checkCer()
    {
        $this->loadModel('g5_certificate')->checkCer();
    }
	
	//验证码
	 public function validn() {
        $checkcode = new validn();
        $checkcode->width = 60;
        $checkcode->height = 30;
        $checkcode->codelength = 4;
        $checkcode->fontSize = 14;
        $checkcode->checkCodeType = 2;
        $checkcode->fontFile = DATA_PATH . "tahoma.ttf";
        $checkcode->pictrues();
        exit;
    }
    
}